/**
 * 侧边栏
 * 侧边栏为单例模式
 * 块由 blocks 数量决定 不包括 toggle & backTop
 * 块默认隐藏到 -50px 后 hide()
 * options : {
 *     toggle : true | false 是否显示回收显示效果
 *     backTop : true | false 是否显示回到顶部
 *     spacing : 两个块之间间距(px)
 *     showHideStyle : 'alone' 显示/隐藏方式 alone - 单个  whole - 整体
 *     size : 'md' 暂时只支持 md
 *     onRight : 块与右边框距离
 *     showSpeed : 显示速度 1000(ms) | 'fast'
 *     hideSpeed : 隐藏速度 1000(ms) | 'fast'
 *     defBlocks : { 默认块配置
 *         toggle : {
 *              color : '#3497db',
 *              bgColor : '#fff'
 *          },
 *          goTop : {
 *              color : '#3497db',
 *              bgColor : '#fff'
 *          }
 *     }
 *     blocks : [
 *         {
 *             color : 内部颜色,
 *             bgColor : 背景颜色,
 *             clazz : 加载在块上的 class,
 *             placeholder : 提示性文字
 *             content : 块中文字 (最好不要超过4个字) 或输入纯英文 会当做font样式处理 在 div 中加入 <i></i>
 *             onClick : 点击事件
 *             onMouseOver : 鼠标滑过事件
 *             onMouseLeave : 鼠标离开事件
 *             onMouseEnter : 鼠标进入事件
 *             panel : 显示frame (暂不支持)
 *         }
 *     ]
 * }
 * @author : Arthur
 */
var SideBar = {
    // 窗口总高
    originHeight: ($(window).height() / 2),
    // 默认显示回到顶部距离
    defBackDistance: 200,
    // 监听器
    listener: [],
    // statue
    isHide: false,
    // 默认配置
    defOptions: {
        toggle: true,
        backTop: true,
        spacing: 55,
        showHideStyle: 'alone',
        size: 'md',
        onRight: 0,
        showSpeed: 'normal',
        hideSpeed: 'normal',
        defBlocks: {
            toggle: {
                color: '#3497db',
                bgColor: '#fff'
            },
            goTop: {
                color: '#3497db',
                bgColor: '#fff'
            }
        },
        blocks: []
    },
    customerOpt: {},
    init: function (tarObj) {
        this.destroy();
        this.windowListener();
        this.customerOpt = $.extend(this.defOptions, tarObj);
        $('body').append(this.setFrame());
        $('#sideBar_frame').css('top', this.originHeight);
        this.setToggle();
        this.setGoTop();
        this.setOptions();
        this.show(0);
    },
    windowListener: function () {
        $(window).resize(function () {
            this.originHeight = $(window).height() / 2;
            $('#sideBar_frame').css('top', this.originHeight);
        });
    },
    setFrame: function () {
        return '<div id="sideBar_frame" class="sideBar_frame"></div>';
    },
    setBlock: function () {
        return '<div class="sideBar_block"></div>';
    },
    setGoTop: function () {
        if (this.customerOpt.backTop) {
            this.customerOpt.blocks.push({
                color: this.customerOpt.defBlocks.goTop.color,
                bgColor: this.customerOpt.defBlocks.goTop.bgColor,
                placeholder: '返回顶部',
                clazz: 'sideBar_block_goTop',
                content: 'fa fa-arrow-up',
                panel: '',
                onClick: function () {
                    $('body,html').animate({scrollTop: 0});
                }
            });
            // 滚动条监听
            $(window).scroll(setBackTopStatus);
            // 监听防止回到头部按钮位置错误
            this.listener.push(
                setInterval(setBackTopStatus, 1500)
            );
        }

        function setBackTopStatus() {
            var $top = $('.sideBar_block_goTop');
            if ($top.prev().is(':hidden')) {
                return;
            }
            if ($(window).scrollTop() >= SideBar.defBackDistance) {
                if ($top.is(':hidden') && !SideBar.isHide) {
                    $top.stop(true).show().animate({right: (SideBar.customerOpt.onRight + 3 + 'px')}, SideBar.customerOpt.showSpeed, function () {
                        $top.animate({right: (SideBar.customerOpt.onRight + 'px')}, 'fast');
                    });
                }
            }
            else {
                if (!$top.is(':hidden')) {
                    $top.stop(true).animate({right: '-50px'}, SideBar.customerOpt.hideSpeed, function () {
                        $top.hide();
                    });
                }
            }
        }
    },
    setToggle: function () {
        if (this.customerOpt.toggle) {
            this.customerOpt.blocks.unshift({
                color: this.customerOpt.defBlocks.toggle.color,
                bgColor: this.customerOpt.defBlocks.toggle.bgColor,
                placeholder: '显示/隐藏',
                content: 'fa fa-chevron-right',
                panel: '',
                onClick: function (dom) {
                    if (SideBar.isHide) {
                        show(dom.currentTarget);
                    } else {
                        hide(dom.currentTarget);
                    }
                }
            });

            function show(dom) {
                SideBar.isHide = false;
                var $dom = $(dom);
                var $i = $dom.find('i');
                $dom.stop().animate({
                    right: (SideBar.customerOpt.onRight + 3 + 'px')
                }, SideBar.customerOpt.showSpeed, function () {
                    $i.fadeOut(function () {
                        $i.removeClass('fa-chevron-left sideBar_block_i_hide').addClass('fa-chevron-right').fadeIn();
                    });
                    $dom.stop().animate({right: (SideBar.customerOpt.onRight + 'px')}, 'fast');
                    SideBar.show(1);
                });
            }

            function hide(dom) {
                SideBar.isHide = true;
                var $dom = $(dom);
                var domWidth = $dom.width();
                var $i = $dom.find('i');
                $dom.stop().animate({
                    right: ('-' + (domWidth / 2))
                }, 'fast', function () {
                    SideBar.hide(1);
                    $i.fadeOut(function () {
                        $i.removeClass('fa-chevron-right').addClass('fa-chevron-left sideBar_block_i_hide').fadeIn();
                    });
                });
            }
        }
    },
    setOptions: function () {
        var $sideBarFrame = $('#sideBar_frame');
        this.customerOpt.blocks.forEach(function (value, index, array) {
            $sideBarFrame.append(SideBar.setBlock());
            var $block = $sideBarFrame.find('div').eq(index);
            if ($block) {

                // 添加事件
                $block.addClass('sideBar_block_' + SideBar.customerOpt.size).addClass(value.clazz).css({
                    'color': value.color,
                    'background-color': value.bgColor,
                    'top': (SideBar.customerOpt.spacing * (index - 1)) + 'px'
                }).on('click', function (obj) {
                    if (typeof value.onClick === 'function')
                        value.onClick(obj);
                }).on('mouseover', function (obj) {
                    if (typeof value.onMouseOver === 'function')
                        value.onMouseOver(obj);
                }).on('mouseenter', function (obj) {
                    if (typeof value.onMouseEnter === 'function')
                        value.onMouseEnter(obj);
                }).on('mouseleave', function (obj) {
                    if (typeof value.onMouseLeave === 'function')
                        value.onMouseLeave(obj);
                });

                // 提示性文字
                if (value.placeholder) {
                    $block.attr('title', value.placeholder);
                }

                // 根据输入文本确定显示中文抑或显示font图标
                if (value.content && SideBar.hasChinese(value.content)) {
                    $block.html('<p>' + value.content + '</p>');
                } else {
                    $block.html('<i class="' + value.content + '  fa-2x" aria-hidden="true"></i>');
                }

                // 设置鼠标点划过状态
                if (typeof value.onClick === 'function') {
                    $block.css({
                        cursor: 'pointer'
                    }).children().css({
                        cursor: 'pointer'
                    });
                }
            }
        });
    },
    show: function (mark) {
        function inWindow(mark) {
            if ($blocks.eq(mark).length > 0 && $blocks.eq(mark).is(':hidden')) {
                // 对返回顶部按钮做特殊处理 返回按钮默认在最后一个
                if ($blocks.eq(mark).hasClass('sideBar_block_goTop') && $(window).scrollTop() < SideBar.defBackDistance) {
                    return;
                }
                $blocks.eq(mark).show().animate({right: (SideBar.customerOpt.onRight + 3 + 'px')}, SideBar.customerOpt.showSpeed, function () {
                    $blocks.eq(mark).animate({right: (SideBar.customerOpt.onRight + 'px')}, 'fast');
                    showOrder(mark);
                });
                showOrder(mark);
            }
        }

        // 显示顺序 用于控制整体 | 独立
        function showOrder(mark) {
            if ('alone' === SideBar.customerOpt.showHideStyle) inWindow(++mark);
            else if ('whole' === SideBar.customerOpt.showHideStyle) inWindow(++mark);
        }

        var $blocks = $('.sideBar_block');
        inWindow(mark);
    },
    hide: function (mark) {
        function outWindow(mark) {
            if ($blocks.eq(mark).length > 0 && !$blocks.eq(mark).is(':hidden')) {
                $blocks.eq(mark).animate({right: '-50px'}, SideBar.customerOpt.hideSpeed, function () {
                    $blocks.eq(mark).hide();
                    hideOrder(mark);
                });
                hideOrder(mark);
            }
        }

        // 隐藏顺序 用于控制整体 | 独立
        function hideOrder(mark) {
            if ('alone' === SideBar.customerOpt.showHideStyle) outWindow(++mark);
            else if ('whole' === SideBar.customerOpt.showHideStyle) outWindow(++mark);
        }

        var $blocks = $('.sideBar_block');
        outWindow(mark);
    },
    hasChinese: function (str) {
        return /^[\u3220-\uFA29]+$/.test(str);
    },
    destroy: function () {
        this.hide(0);
        $('#sideBar_frame').remove();
        this.listener.forEach(function (value) {
            clearInterval(value);
        });
        this.listener = [];
    }
};